#include "com_bft_BitFetchTable.h"
#include "bittablemanager.h"

JNIEXPORT jint JNICALL Java_com_bft_BitFetchTable_createDiskTable
  (JNIEnv *env, jobject obj, jstring jpath)
{
    jclass thiz = env->GetObjectClass(obj);

    jfieldID id = env->GetFieldID(thiz, "handle", "J");
    jlong jhandle_p = env->GetLongField(obj, id);
    long handle_p = jhandle_p;
    if (handle_p != 0)
        return -1;

    id = env->GetFieldID(thiz, "name", "Ljava/lang/String;");
    jstring jname = (jstring)env->GetObjectField(obj, id);

    std::string path = env->GetStringUTFChars(jpath, 0);
    std::string name = env->GetStringUTFChars(jname, 0);
    BitHashTableHandle *handle = BitTableManager::instance()->createDiskTableHandle(path, name);

    handle_p = (long) handle;
    id = env->GetFieldID(thiz, "handle", "J");
    env->SetLongField(obj, id, handle_p);

    return 0;
}

JNIEXPORT jint JNICALL Java_com_bft_BitFetchTable_createMemTable
  (JNIEnv *env, jobject obj)
{
    jclass thiz = env->GetObjectClass(obj);

    jfieldID id = env->GetFieldID(thiz, "handle", "J");
    jlong jhandle_p = env->GetLongField(obj, id);
    long handle_p = jhandle_p;
    if (handle_p != 0)
        return -1;

    id = env->GetFieldID(thiz, "name", "Ljava/lang/String;");
    jstring jname = (jstring)env->GetObjectField(obj, id);

    std::string name = env->GetStringUTFChars(jname, 0);
    BitHashTableHandle *handle = BitTableManager::instance()->createMemTableHandle(name);
    if (handle == NULL)
        return -1;

    handle_p = (long) handle;

    id = env->GetFieldID(thiz, "handle", "J");
    env->SetLongField(obj, id, handle_p);

    return 0;
}

JNIEXPORT jint JNICALL Java_com_bft_BitFetchTable_destoryTable
  (JNIEnv *env, jobject obj)
{
    jclass thiz = env->GetObjectClass(obj);

    jfieldID id = env->GetFieldID(thiz, "handle", "J");
    jlong jhandle_p = env->GetLongField(obj, id);
    long handle_p = jhandle_p;
    if (handle_p == 0)
        return -1;

    id = env->GetFieldID(thiz, "name", "Ljava/lang/String;");
    jstring jname = (jstring)env->GetObjectField(obj, id);

    std::string name = env->GetStringUTFChars(jname, 0);
    BitTableManager::instance()->destoryTableHandle(name);
    env->SetLongField(obj, id, 0);

    return 0;
}

JNIEXPORT void JNICALL Java_com_bft_BitFetchTable_clearTable
  (JNIEnv *env, jobject obj)
{
    jclass thiz = env->GetObjectClass(obj);

    jfieldID id = env->GetFieldID(thiz, "handle", "J");
    jlong jhandle_p = env->GetLongField(obj, id);
    long handle_p = jhandle_p;
    BitHashTableHandle *handle = (BitHashTableHandle *)handle_p;
    if (handle == NULL)
        return ;

    handle->clear();
}

JNIEXPORT void JNICALL Java_com_bft_BitFetchTable_setBit
  (JNIEnv *env, jobject obj, jstring str)
{
    jclass thiz = env->GetObjectClass(obj);
    jfieldID id = env->GetFieldID(thiz, "handle", "J");
    jlong jhandle_p = env->GetLongField(obj, id);
    long handle_p = jhandle_p;
    BitHashTableHandle *handle = (BitHashTableHandle *)handle_p;
    if (handle == NULL)
        return ;

    HashableString hstr(env->GetStringUTFChars(str, 0));
    handle->setBit(hstr);
}

JNIEXPORT void JNICALL Java_com_bft_BitFetchTable_clearBit
  (JNIEnv *env, jobject obj, jstring str)
{
    jclass thiz = env->GetObjectClass(obj);
    jfieldID id = env->GetFieldID(thiz, "handle", "J");
    jlong jhandle_p = env->GetLongField(obj, id);
    long handle_p = jhandle_p;
    BitHashTableHandle *handle = (BitHashTableHandle *)handle_p;
    if (handle == NULL)
        return ;

    HashableString hstr(env->GetStringUTFChars(str, 0));
    handle->clearBit(hstr);
}

JNIEXPORT jboolean JNICALL Java_com_bft_BitFetchTable_testBit
  (JNIEnv *env, jobject obj, jstring str)
{
    jclass thiz = env->GetObjectClass(obj);
    jfieldID id = env->GetFieldID(thiz, "handle", "J");
    jlong jhandle_p = env->GetLongField(obj, id);
    long handle_p = jhandle_p;
    BitHashTableHandle *handle = (BitHashTableHandle *)handle_p;
    if (handle == NULL)
        return false;

    HashableString hstr(env->GetStringUTFChars(str, 0));
    return handle->testBit(hstr);
}

